﻿using AutoMapper;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using SIMS.Shared.DTO;
using SIMS.Shared.Models;
using SIMS.SqlSugarWebApi.Models;
using SIMS.SqlSugarWebApi.ServiceInterface;

namespace SIMS.SqlSugarWebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CourseController: ControllerBase
    {
        private readonly ICourseService _service;
        private readonly IMapper _mapper;

        public CourseController(ICourseService service, IMapper mapper)
        {
            _service = service;
            _mapper = mapper;
        }

        [HttpPost]
        public async Task<IActionResult> AddCourse(CourseDTO c)
        {
            var course = _mapper.Map<Course>(c);
            var data = await _service.AddEntity(course);

            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpDelete]
        public async Task<IActionResult> DeleteCourse(int id)
        {
            var data = await _service.DeleteEntity(id);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpGet]
        public async Task<IActionResult> GetCourse(int id)
        {
            var data = await _service.GetEntity(id);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpPut]
        public async Task<IActionResult> UpdateCourse(CourseDTO c)
        {
            var course = _mapper.Map<Course>(c);
            var data = await _service.UpdateEntity(course);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }

        [HttpGet]
        public async Task<IActionResult> GetCourses(int pageNum, int pageSize, string? courseName = null, string? teacher = null)
        {
            var data = await _service.GetCourses(courseName, teacher, pageNum, pageSize);
            var res = new ResponseResultModel() { data = data };
            return Ok(res);
        }
    }
}
